<template>
	<view class="container">
		<uni-forms :model="rich">
			<uni-forms-item class="form-item" label="植物种类" label-width="150rpx">
				<uni-easyinput type="text" :disabled="isview" v-model="rich.zbzl" />
			</uni-forms-item>

			<uni-forms-item class="form-item" label="对角线1" label-width="150rpx">
				<uni-easyinput type="text" :disabled="isview" @change="change" v-model="rich.djx1" /><strong></strong>
			</uni-forms-item>
			<uni-forms-item class="form-item" label="对角线2" label-width="150rpx">
				<uni-easyinput type="text" :disabled="isview" @change="change" v-model="rich.djx2" />
			</uni-forms-item>
			<uni-forms-item class="form-item" label="总数" label-width="150rpx">
				<uni-easyinput type="text" :disabled="true" v-model="rich.zs" />
			</uni-forms-item>
			<button type="primary" @click="submit()">{{title}}</button>
		</uni-forms>

	</view>
</template>

<script>
	import {
		getInfo,
		setInfo
	} from '@/core/unicommon/storyageInfo.js'
	import {
		addOrUpdateRichness,
		getRichness
	} from "@/api/system.js"

	export default {
		data() {
			return {
				title: "提交",
				isview: true,
				rich: {
					"djx1": "",
					"djx2": "",
					"ydNumber": "",
					"yfNumber": "",
					"zbzl": "",
					"zs": ""
				},
				richs: [],
				ydNumber: "",
				richDatas: [],
				userdata: null


			}
		},
		onLoad(e) {

			if (e && e.data) {
				let data = JSON.parse(decodeURIComponent(e.data))

				this.rich.ydNumber = data.ydNumber
				if (data.type == 'add') {
					this.isview = false
					this.rich.action = "creat"
				} else {
					this.isview = true
					this.rich = data
				}
				getInfo("userdata").then(res => {
					this.userdata = res.data
					this.richDatas = res.data.rich

				})
			}
			this.getNetWork()
		},
		methods: {
			onNavigationBarButtonTap(e) {
				uni.navigateTo({
					url: "/pages/sampleplot/sampleplot/richlist?data="+ encodeURIComponent(this.rich.ydNumber)
				})
			
			},
			change() {
				if (this.rich.djx1 && this.rich.djx2) {
					this.rich.zs = parseFloat(this.rich.djx1) + parseFloat(this.rich.djx2)
				}
			},
			getNetWork() {
				let that = this
				uni.getNetworkType({
					success: res => {
						if (res.networkType === 'none') {
							that.network = false
						} else {
							that.network = true
						}
					}
				})
			},
			async submit() {
				if (this.isview) {
					this.title = "提交"
					this.isview = false

				} else {

					let index = this.richDatas.findIndex(t => t.ydNumber == this.rich.ydNumber && t.zbzl == this.rich
						.zbzl)
					if (index>0 && this.rich.action == 'creat') {
						uni.showToast({
							title: '已经存在相同数据',
							icon: 'none'
						})
						return
					}
					this.rich.action = "none"
					if (this.network) {
						await addOrUpdateRichness(this.rich)

						this.isview = true
						this.title = "编辑"
						let richdata = await getRichness()
						console.log("12",richdata)
						this.richDatas = richdata.data.map(d => {
							d["action"] = 'none'
							return d
						})
						console.log("11",this.richDatas)
						//获取列表

					} else {
						this.rich.action = "edit"
						if (index) {
							this.richDatas[index] = this.rich
						} else {
							this.richDatas.push(this.rich)
						}
					}





					this.userdata.rich = this.richDatas
					setInfo('userdata', this.userdata)

					this.isview = true
					this.title = "编辑"
				}



			}

		}
	}
</script>

<style scoped>
	.form-item {
		padding: 10rpx;

	}

	.tag-view {
		float: left;
		display: flex;
		margin: 10rpx;
	}

	.item-image {
		width: 60rpx;
		height: 60rpx;
	}
</style>